﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Mvc;
using Database;

namespace Client.Controllers
{
    public class HomeController : Controller
    {
        HttpClient client;

        
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(string tenphim, string ngay)
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:1818/");
            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
            tenphim = tenphim.ToUpper();
            var str = LayChu(tenphim);
            var products = new List<Galaxy>();
            var list = new List<Galaxy>();
            HttpResponseMessage response = client.GetAsync("api/galaxy").Result; // Blocking call!
            if (response.IsSuccessStatusCode)
            {
                // Parse the response body. Blocking!
                products = response.Content.ReadAsAsync<List<Galaxy>>().Result;
                if (ngay == "")
                    ngay = "10/10/1001";
                var d = 10;
                var m = 10;
                var y = 1001;               
                d = Int32.Parse(ngay.Substring(0, 2));
                m = Int32.Parse(ngay.Substring(3, 2));
                y = Int32.Parse(ngay.Substring(6, 4));               
                
                //var d = 10;
                //var m = 10;
                //var y = 1000;
                DateTime date;
                try
                {
                    date = new DateTime(y, m, d).Date;
                }
                catch (Exception ex)
                {
                    date = new DateTime(1001, 10, 10).Date;
                }
                foreach (var f in products)
                {
                    foreach (var s in str)
                    {
                        if (f.Ten.IndexOf(s) >= 0)
                        {
                            if (ngay == "10/10/1001")
                            {
                                DateTime today = DateTime.Now.Date;
                                d = today.Day;
                                m = today.Month;
                                y = today.Year;
                                DateTime d1 = new DateTime(y, m, d + 1).Date;
                                DateTime d2 = new DateTime(y, m, d + 2).Date;
                                DateTime d3 = new DateTime(y, m, d + 3).Date;
                                DateTime d4 = new DateTime(y, m, d + 4).Date;
                                if (((DateTime.Compare(today, GalaxyData.LayNgay(f.Khoichieu)[0]) == 1 || DateTime.Compare(today, GalaxyData.LayNgay(f.Khoichieu)[0]) == 0) &&
                                    (DateTime.Compare(today, GalaxyData.LayNgay(f.Khoichieu)[1]) == -1 || DateTime.Compare(today, GalaxyData.LayNgay(f.Khoichieu)[1]) == 0)) ||
                                    ((DateTime.Compare(d1, GalaxyData.LayNgay(f.Khoichieu)[0]) == 1 || DateTime.Compare(d1, GalaxyData.LayNgay(f.Khoichieu)[0]) == 0) &&
                                    (DateTime.Compare(d1, GalaxyData.LayNgay(f.Khoichieu)[1]) == -1 || DateTime.Compare(d1, GalaxyData.LayNgay(f.Khoichieu)[1]) == 0)) ||
                                    ((DateTime.Compare(d2, GalaxyData.LayNgay(f.Khoichieu)[0]) == 1 || DateTime.Compare(d2, GalaxyData.LayNgay(f.Khoichieu)[0]) == 0) &&
                                    (DateTime.Compare(d2, GalaxyData.LayNgay(f.Khoichieu)[1]) == -1 || DateTime.Compare(d2, GalaxyData.LayNgay(f.Khoichieu)[1]) == 0)) ||
                                    ((DateTime.Compare(d3, GalaxyData.LayNgay(f.Khoichieu)[0]) == 1 || DateTime.Compare(d3, GalaxyData.LayNgay(f.Khoichieu)[0]) == 0) &&
                                    (DateTime.Compare(d3, GalaxyData.LayNgay(f.Khoichieu)[1]) == -1 || DateTime.Compare(d3, GalaxyData.LayNgay(f.Khoichieu)[1]) == 0)) ||
                                    ((DateTime.Compare(d4, GalaxyData.LayNgay(f.Khoichieu)[0]) == 1 || DateTime.Compare(d4, GalaxyData.LayNgay(f.Khoichieu)[0]) == 0) &&
                                    (DateTime.Compare(d4, GalaxyData.LayNgay(f.Khoichieu)[1]) == -1 || DateTime.Compare(d4, GalaxyData.LayNgay(f.Khoichieu)[1]) == 0)))
                                {
                                    list.Add(f);
                                }
                            }
                            else
                            {
                                if (((DateTime.Compare(date, GalaxyData.LayNgay(f.Khoichieu)[0]) == 1 || DateTime.Compare(date, GalaxyData.LayNgay(f.Khoichieu)[0]) == 0) &&
                                    (DateTime.Compare(date, GalaxyData.LayNgay(f.Khoichieu)[1]) == -1 || DateTime.Compare(date, GalaxyData.LayNgay(f.Khoichieu)[1]) == 0)))
                                {
                                    list.Add(f);
                                }
                            }
                            break;
                        }
                    }

                }
                ViewBag.Galaxy = list;

            }
            else
            {

            }
            return View();
            
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public List<string> LayChu(string str)
        {
            var lst = new List<string>();
            for (int i = 0; i < str.Length;)
            {
                var t = 0;
                var count = -1;
                for (int j = i; j < str.Length - 1; j++)
                {
                    count++;
                    if (str[j] == ' ' && str[j+1] != ' ')
                    {
                        var tr = str.Substring(i, count - t);
                        lst.Add(tr);
                        i = j + 1;
                        break;
                    }
                    else if (str[j] == ' ' && str[j + 1] == ' ')
                    {
                        t++;
                    }
                    else {
                        if (j == str.Length - 2)
                        {
                            var tr = str.Substring(i, count - t + 2);
                            lst.Add(tr);
                            i = str.Length;
                            
                        }
                    }
                }
            }
                return lst;
        }
    }
}
